<?php

namespace common\modules\wechat\models\backend;

use common\helpers\Html;
use common\widgets\grid\ActionColumn;
use common\widgets\grid\FormulaColumn;
use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;

/**
 * 回复规则后台搜索
 *
 * @package common\modules\wechat\models\backend
 */
class ReplyRuleSearch extends ReplyRule
{

    public $keyword;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'wid', 'status'], 'integer'],
            [['name', 'mid', 'priority'], 'safe'],
            //[['keyword'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/wechat', 'Reply List');
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'keyword' => '触发关键词',
        ]);
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = self::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'priority' => $this->priority,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'mid', $this->mid]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [
            [
                'attribute' => 'id',
                'options' => [
                    'width' => 75
                ]
            ],
            'name',
            [
                'attribute' => 'mid',
                'value' => function($model) {
                    /** @var static $model */
                    /** @var Module $module */
                    $module = $model->module;
                    if ($module)
                    {
                        return $module->name;
                    }
                    return $model->mid;
                },
                'width' => '200px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => ArrayHelper::map(Module::find()->all(), 'id', 'name'),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择模块'],
            ],
            [
                'attribute' => 'status',
                'format' => 'html',
                'value' => function($model) {
                    return Html::tag('span', ReplyRule::$statuses[$model->status], [
                        'class' => 'label label-' . ($model->status == ReplyRule::STATUS_ACTIVE ? 'success' : 'info')
                    ]);
                },
                'width' => '150px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => self::$statuses,
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择状态'],
            ],
            [
                'attribute' => 'keyword',
                'format' => 'html',
                'value' => function($model) {
                    return implode(' ', array_map(function($model) {
                        return Html::tag('code', $model->keyword);
                    }, $model->keywords));
                },
            ],

            [
                'attribute' => 'priority',
                'options' => [
                    'width' => 70
                ]
            ],

            [
                'class' => FormulaColumn::className(),
                'attribute' => 'created_at',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'width' => '180px',
                'hAlign' => GridView::ALIGN_CENTER,
                'mergeHeader' => true,
            ],

            [
                'class' => ActionColumn::className(),
                'template' => '{update} {delete}',
                'options' => [
                    'width' => 80
                ]
            ],
        ];
    }
}
